/**
 * 
 */
package com.example.radioclub.login.google.tasks;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.example.radioclub.data.Constants;
import com.example.radioclub.login.google.interfaces.IGoogleUserInfoTask;
import com.example.radioclub.music.helper.ClientRequestFactory;

import android.os.AsyncTask;
import android.util.Log;

/**
 * This one is for fetching Google User Info. Hopefully, this return something
 * in JSON format.
 * 
 * @author wizard
 *
 */
public class GoogleUserInfoTask extends AsyncTask<Void, Void, String>{
	private final String TAG = Constants.APP_TAG + getClass().getName();
	private IGoogleUserInfoTask listener;
	private RestTemplate restTemplate;
	
	public GoogleUserInfoTask(String googleUserInfoAccessToken, IGoogleUserInfoTask listener) {
		this.restTemplate = new RestTemplate(true, new ClientRequestFactory("Authorization", "OAuth " + googleUserInfoAccessToken));
		this.listener = listener;
	}
	
	@Override
	protected String doInBackground(Void... params) {
		// TODO Auto-generated method stub
		if(this.restTemplate != null) {
			ResponseEntity<String> response = this.restTemplate.getForEntity("https://www.googleapis.com/oauth2/v2/userinfo", String.class);
			// Log.i(TAG, response.getBody());
			return response.getBody();
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		// Log.i(TAG, (result != null ? result : "Nothing here, Sorry."));
		this.listener.onUserInfoAcquired(result);
		super.onPostExecute(result);
	}
}
